# _*_ encoding:utf-8 _*_
"""
课程的模型
"""
# python 源生包
from datetime import datetime
# 第三方库
from django.db import models
from organization.models import CourseOrg
# 自定义的包
# Create your models here.


# 课程的模型
class Course(models.Model):
    course_org = models.ForeignKey(CourseOrg, verbose_name="课程机构", null=True, blank=True, on_delete=models.DO_NOTHING)
    name = models.CharField(max_length=50, verbose_name="课程名")
    desc = models.CharField(max_length=300, verbose_name="课程描述")
    detail = models.TextField(verbose_name="课程详情")
    degree = models.CharField(choices=(("cj", "初级"), ("zj", "中级"), ("gj", "高级")), max_length=10, verbose_name="课程难度")
    learn_times = models.IntegerField(default=0, verbose_name="学习时长(分钟)")
    students = models.IntegerField(default=0, verbose_name="学习人数")
    fav_nums = models.IntegerField(default=0, verbose_name="收藏人数")
    image = models.ImageField(upload_to="courses/%Y/%m", verbose_name="封面图", max_length=100)
    click_nums = models.IntegerField(default=0, verbose_name="课程点击量")
    add_time = models.DateField(default=datetime.now, verbose_name="添加时间")
    tag = models.CharField(default="", verbose_name="课程标签", max_length=10)

    class Meta:
        verbose_name = "课程"
        verbose_name_plural = verbose_name

    # 因为课程是章节的外键，所以可以通过以下方法，反向获取外键的数据
    def get_zj_nums(self):
        # self.lesson_set 为 django 自动生成
        # self.lesson_set.all() 是一个 query dict
        # 视图中可以通过 course.get_zj_nums() 调用
        all_lessons = self.lesson_set.all().count()
        return all_lessons

    # 因为课程是学生的外键，所以可以通过以下方法，反向获取外键的数据
    def get_learn_users(self):
        return self.usercourse_set.all()[:5]

    # 如果这个 model 为其它 model 的外键，需要加入这个，不然会显示 object
    def __str__(self):
        return self.name


# 章节
class Lesson(models.Model):
    course = models.ForeignKey(Course, verbose_name="课程", on_delete=models.DO_NOTHING)  # 外键
    name = models.CharField(max_length=100, verbose_name="章节名")
    add_time = models.DateField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "章节"
        verbose_name_plural = verbose_name

    # 如果这个 model 为其它 model 的外键，需要加入这个，不然会显示 object
    def __str__(self):
        return self.name


# 视频
class Video(models.Model):
    lesson = models.ForeignKey(Lesson, verbose_name="章节", on_delete=models.DO_NOTHING)  # 外键
    name = models.CharField(max_length=100, verbose_name="视频名")
    add_time = models.DateField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "视频"
        verbose_name_plural = verbose_name


# 课程资源
class CourseResource(models.Model):
    course = models.ForeignKey(Course, verbose_name="课程", on_delete=models.DO_NOTHING)  # 外键
    name = models.CharField(max_length=100, verbose_name="名称")
    download = models.FileField(upload_to="course/resource/%Y/%m", verbose_name="资源文件", max_length=100)
    add_time = models.DateField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "课程资源"
        verbose_name_plural = verbose_name
